package com.jeesite.modules.exam.quota.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeesite.common.lang.DateUtils;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.modules.exam.assessment.entity.DcSupExamInfo;
import com.jeesite.modules.exam.assessment.service.DcSupExamInfoService;
import com.jeesite.modules.exam.quota.entity.DcSupExamQuotaInfo;
import com.jeesite.modules.exam.quota.entity.DcSupExamSumQuotaInfo;
import com.jeesite.modules.exam.socreResult.entity.DcSupExamGroup;
import com.jeesite.modules.sys.entity.User;
import com.jeesite.modules.sys.utils.UserUtils;
import com.jeesite.modules.utils.OfficeUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.exam.quota.entity.DcSupExamQuota;
import com.jeesite.modules.exam.quota.service.DcSupExamQuotaService;

import java.util.List;
import java.util.Map;

/**
 * 考核任务指标表Controller
 * @author xhc
 * @version 2022-05-28
 */
@Controller
@RequestMapping(value = "${adminPath}/quota/dcSupExamQuota")
public class DcSupExamQuotaController extends BaseController {

	@Autowired
	private DcSupExamQuotaService dcSupExamQuotaService;

	@Autowired
	private DcSupExamInfoService dcSupExamInfoService;
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public DcSupExamQuota get(String id, boolean isNewRecord) {
		return dcSupExamQuotaService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequestMapping(value = {"list", ""})
	public String list(DcSupExamQuota dcSupExamQuota, Model model) {
		dcSupExamQuota.setContactName(StringUtils.isBlank(dcSupExamQuota.getContactName())? UserUtils.getUser().getUserName():dcSupExamQuota.getContactName());
		dcSupExamQuota.setContactPhone(StringUtils.isBlank(dcSupExamQuota.getContactPhone())? UserUtils.getUser().getPhone():dcSupExamQuota.getContactPhone());
		model.addAttribute("dcSupExamQuota", dcSupExamQuota);
		Integer currentYear = Integer.parseInt(DateUtils.getYear());
		model.addAttribute("yearList", OfficeUtil.getYearList(currentYear));
		model.addAttribute("currentYear", currentYear);
		DcSupExamQuotaInfo dcSupExamQuotaInfo = new DcSupExamQuotaInfo();
		dcSupExamQuotaInfo.setExameOrg(dcSupExamQuota.getCreateOrg());
		model.addAttribute("dcSupExamQuotaInfo",dcSupExamQuotaInfo);
		if(StringUtils.equals("2",dcSupExamQuota.getSubmitStatus())){
			if(StringUtils.equals("1",dcSupExamQuota.getQuotaGroup())){
				return "modules/exam/quota/dcSupExamQuotaListShow";
			}else {
				return "modules/exam/quota/dcSupExamQuotaListSingleShow";
			}

		}
		if(StringUtils.equals("1",dcSupExamQuota.getQuotaGroup())) {
			return "modules/exam/quota/dcSupExamQuotaList";
		}

		 return "modules/exam/quota/dcSupExamQuotaListSingle";
	}


	/**
	 *
	 *考核任务
	 * @return
	 */
	@RequestMapping("listExamInfo")
	@ResponseBody
	public Object listExamInfo(DcSupExamInfo dcSupExamInfo) {
		dcSupExamInfo.setExamYear((Integer.parseInt(dcSupExamInfo.getExamYear())-1)+"");
		dcSupExamInfo.setExamStatus("2");
		List<DcSupExamInfo> list = dcSupExamInfoService.findQuotaList(dcSupExamInfo);
		return list;
	}


	@RequestMapping("file")
	public String quotalFile(DcSupExamQuota dcSupExamQuota, Model model) {
		dcSupExamQuota.setContactName(StringUtils.isBlank(dcSupExamQuota.getContactName())? UserUtils.getUser().getUserName():dcSupExamQuota.getContactName());
		dcSupExamQuota.setContactPhone(StringUtils.isBlank(dcSupExamQuota.getContactPhone())? UserUtils.getUser().getPhone():dcSupExamQuota.getContactPhone());
		model.addAttribute("dcSupExamQuota", dcSupExamQuota);
		Integer currentYear = Integer.parseInt(DateUtils.getYear());
		model.addAttribute("yearList", OfficeUtil.getYearList(currentYear));
		model.addAttribute("currentYear", currentYear);
		model.addAttribute("dcSupExamQuotaInfo", new DcSupExamQuotaInfo());
		return "modules/exam/quota/dcSupExamQuotaListSingleFile";
	}
	@RequestMapping("filed")
	public String quotalFiled(DcSupExamQuota dcSupExamQuota, Model model) {
		dcSupExamQuota.setContactName(StringUtils.isBlank(dcSupExamQuota.getContactName())? UserUtils.getUser().getUserName():dcSupExamQuota.getContactName());
		dcSupExamQuota.setContactPhone(StringUtils.isBlank(dcSupExamQuota.getContactPhone())? UserUtils.getUser().getPhone():dcSupExamQuota.getContactPhone());
		model.addAttribute("dcSupExamQuota", dcSupExamQuota);
		Integer currentYear = Integer.parseInt(DateUtils.getYear());
		model.addAttribute("yearList", OfficeUtil.getYearList(currentYear));
		model.addAttribute("currentYear", currentYear);
		model.addAttribute("dcSupExamQuotaInfo", new DcSupExamQuotaInfo());
		return "modules/exam/quota/dcSupExamQuotaListSingleFileUpload";
	}

	@RequestMapping("version")
	public String version(DcSupExamQuota dcSupExamQuota, Model model) {
		dcSupExamQuota.setContactName(StringUtils.isBlank(dcSupExamQuota.getContactName())? UserUtils.getUser().getUserName():dcSupExamQuota.getContactName());
		dcSupExamQuota.setContactPhone(StringUtils.isBlank(dcSupExamQuota.getContactPhone())? UserUtils.getUser().getPhone():dcSupExamQuota.getContactPhone());
		model.addAttribute("dcSupExamQuota", dcSupExamQuota);
		Integer currentYear = Integer.parseInt(DateUtils.getYear());
		model.addAttribute("yearList", OfficeUtil.getYearList(currentYear));
		model.addAttribute("currentYear", currentYear);
		model.addAttribute("dcSupExamSumQuotaInfo", new DcSupExamSumQuotaInfo());
		return "modules/exam/quota/dcSupExamQuotaListVersion";
	}

	@RequestMapping("danVersion")
	public String danVersion(DcSupExamQuota dcSupExamQuota, Model model) {
		dcSupExamQuota.setContactName(StringUtils.isBlank(dcSupExamQuota.getContactName())? UserUtils.getUser().getUserName():dcSupExamQuota.getContactName());
		dcSupExamQuota.setContactPhone(StringUtils.isBlank(dcSupExamQuota.getContactPhone())? UserUtils.getUser().getPhone():dcSupExamQuota.getContactPhone());
		model.addAttribute("dcSupExamQuota", dcSupExamQuota);
		Integer currentYear = Integer.parseInt(DateUtils.getYear());
		model.addAttribute("yearList", OfficeUtil.getYearList(currentYear));
		model.addAttribute("currentYear", currentYear);
		model.addAttribute("dcSupExamSumQuotaInfo", new DcSupExamSumQuotaInfo());
		return "modules/exam/quota/dcSupExamQuotaListSingleVersion";
	}

	/**
	 * 查询列表数据
	 */
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<DcSupExamQuota> listData(DcSupExamQuota dcSupExamQuota, HttpServletRequest request, HttpServletResponse response) {
		dcSupExamQuota.setPage(new Page<>(request, response));
		Page<DcSupExamQuota> page = dcSupExamQuotaService.findPage(dcSupExamQuota);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("quota:dcSupExamQuota:view")
	@RequestMapping(value = "form")
	public String form(DcSupExamQuota dcSupExamQuota, Model model) {
		model.addAttribute("dcSupExamQuota", dcSupExamQuota);
		return "modules/exam/quota/dcSupExamQuotaForm";
	}

	/**
	 * 保存数据
	 */

	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated DcSupExamQuota dcSupExamQuota) {
		dcSupExamQuota.setSubmitStatus("2");
		dcSupExamQuota.setUpdateBy(UserUtils.getUser().getUserCode());
		dcSupExamQuotaService.save(dcSupExamQuota);
		return renderResult(Global.TRUE, text("保存考核任务指标表成功！"));
	}
	
	/**
	 * 停用数据
	 */
	@RequiresPermissions("quota:dcSupExamQuota:edit")
	@RequestMapping(value = "disable")
	@ResponseBody
	public String disable(DcSupExamQuota dcSupExamQuota) {
		dcSupExamQuota.setStatus(DcSupExamQuota.STATUS_DISABLE);
		dcSupExamQuotaService.updateStatus(dcSupExamQuota);
		return renderResult(Global.TRUE, text("停用考核任务指标表成功"));
	}
	
	/**
	 * 启用数据
	 */
	@RequiresPermissions("quota:dcSupExamQuota:edit")
	@RequestMapping(value = "enable")
	@ResponseBody
	public String enable(DcSupExamQuota dcSupExamQuota) {
		dcSupExamQuota.setStatus(DcSupExamQuota.STATUS_NORMAL);
		dcSupExamQuotaService.updateStatus(dcSupExamQuota);
		return renderResult(Global.TRUE, text("启用考核任务指标表成功"));
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("quota:dcSupExamQuota:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(DcSupExamQuota dcSupExamQuota) {
		dcSupExamQuotaService.delete(dcSupExamQuota);
		return renderResult(Global.TRUE, text("删除考核任务指标表成功！"));
	}
	
}